object gcd_tail_fact {
/*
 * Tail Recursion
 */
	def gcd(a: Int, b: Int): Int = { 
		println("gcd called a = " + a + " b = " + b)
		if (b == 0) a 
		else gcd(b, a % b)
	}
	
/*
 * Exercise 4.6.1 Design a tail-recursive version of factorial.
 */
	def fact( n: BigInt): BigInt = {
		def my_fact(n: BigInt, a: BigInt, f: BigInt ): BigInt = {
			//println("my_fact( n=" + n + " a=" + a + " f=" + f + " )")
			
			if( a == n+1 ) f
			else my_fact(n, a+1, f*a )
		}
		my_fact(n, 1, 1)
	}

	/*
	 * Tester
	 */
	def main( args: Array[String]){
		println(fact(150))
	}
}